<template>
  <div class="comment">
    <div class="right" v-if="comment.pid == '0'">
      <div class="dianzan flex-jac" @click="updateLive('live')">
        <SvgIcon name="dianzan" width="26px" height="26px" :color="comment.isLive ? '#ff6348' : '#000'" />
      </div>
      <span class="live-count" v-if="comment.liveCount > 0">{{ comment.liveCount }}</span>
      <div class="cai flex-jac" @click="updateLive('cai')">
        <SvgIcon name="dianzan" width="26px" height="26px" :color="comment.isCai ? '#ff6348' : '#000'" />
      </div>
    </div>
    <div class="comment__header flex-ac">
      <img src="../../assets/vue.svg" class="avatar" />
      <span class="username">username</span>
      <span class="spag"></span>
      <span class="date">发布于1月1日</span>
    </div>
    <div class="comment__content">{{ comment.content }}</div>
    <div class="comment__footer flex">
      <div class="action flex-jac" v-if="comment.pid == '0'">
        <div class="dianzan flex-ac" @click="updateLive('live')">
          <SvgIcon name="dianzan" width="20px" height="20px" :color="comment.isLive ? '#ff6348' : '#000'" />
          <span>赞 </span>
        </div>
        <div class="cai flex-ac" @click="updateLive('cai')">
          <SvgIcon name="dianzan" style="transform:rotate(180deg)" width="20px" height="20px"
            :color="comment.isCai ? '#ff6348' : '#000'" />
          <span>踩</span>
        </div>
      </div>
      <div class="reply">
        <span>回复</span>
      </div>
      <div class="comment__footer__chidrens" v-if="comment.children && comment.children.length > 0">
        <div class="comment__chidren" v-for=" item in  comment.children " v-bind="item" :key="item.id">
          <div class="comment__chidren__header flex-ac">
            <div class="content">
              <span class="my">{{ comment.user.nickname }}</span>:
              <span class="no-my">@{{ item.user.nickname }}</span>
              {{ item.content }}
            </div>
          </div>
          <div class="comment__chidren__footer flex-ac">
            <div class="dianzan flex-ac" @click="updateLive('live', item)">
              <SvgIcon name="dianzan" width="18px" height="18px" :color="item.isLive ? '#ff6348' : '#000'" />
              <span class="live-count" v-if="item.liveCount > 0">{{ item.liveCount }}</span>
            </div>
            <span class="drop"></span>
            <span class="reply">回复</span>
            <span class="drop"></span>
            <span class="date">{{ item.createTime }}</span>
          </div>
        </div>
        <div class="more flex-ac" v-if="comment.children.length > 3">
          <span>共{{ comment.children.length }}条回复</span>
          <SvgIcon name="xiangyou1" color="#6c5ce7" width="20px" height="20px" />
        </div>
      </div>

    </div>

  </div>
</template>

<script setup lang="ts">
import { IComment } from '@/interface/IComment';
const props = defineProps<{ comment: IComment }>()
function updateLive(type: "live" | "cai", item: IComment = props.comment) {
  if (type == "live") {
    if (item.isLive) {
      item.isLive = false;
      item.liveCount--;
      return;
    }
    item.isLive = true;
    item.liveCount++;
    item.isCai = false;
  } else {
    if (item.isCai) {
      item.isCai = false;
      return;
    }
    item.isCai = true;
    if (item.isLive) {
      item.liveCount--;
    }
    item.isLive = false;
  }
}
</script>

<style lang="scss" scoped>
.comment {
  display: flex;
  position: relative;
  flex-direction: column;
  gap: 20px;
  padding: 18px 16px;
  border-bottom: 1px solid #efefef;

  .right {
    position: absolute;
    left: -60px;
    top: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;

    .dianzan,
    .cai {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      border: 2px solid $color;
      background-color: #fff;
    }

    .cai {
      transform: rotate(180deg);
    }

    .live-count {
      font-size: 24px;
    }

  }

  &__content {
    font-size: 20px;
  }

  &__header {
    font-size: 14px;
    gap: 6px;

    .avatar {
      width: 40px;
      height: 40px;
      object-fit: cover;
      border-radius: 50%;
      border: 1px solid #ccc;
      margin-right: 8px;
    }

    .username {
      font-weight: bold;
      font-size: 20px;
      color: $color;
    }

    .spag {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background-color: #ccc;
    }
  }

  &__footer {
    flex-direction: column;
    gap: 10px;

    .action {

      >div {
        // background-color: red;
        border: 1px solid #eee;
        padding: 6px 18px;
        gap: 10px;

        &.dianzan {
          border-radius: 35px 0 0 35px;

        }


        &.cai {
          border-radius: 0 35px 35px 0;
        }
      }
    }

    .reply {
      font-size: 20px;
      color: #727B82;
    }

    &__chidrens {
      display: flex;
      flex-direction: column;
      background-color: #f8f9fa;
      border-radius: 6px;

    }
  }
}

.comment__chidren {
  display: flex;
  position: relative;
  flex-direction: column;
  gap: 6px;
  font-size: 18px;
  padding: 20px;

  &__header {
    gap: 6px;

    >.content {
      font-size: 20px;
      padding: 8px 0 0;
      line-height: 1.6;

      >.my {
        font-size: 20px;
        color: $color;
        font-weight: bold;
      }

      >.no-my {
        color: #0a987a;
        font-size: 18px;
        margin-right: 10px;

      }
    }
  }

  &__footer {
    gap: 10px;

    .drop {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background-color: #ccc;
    }

    .live-count {
      margin-left: 6px;
    }

    .date {
      color: #727B82;
      font-size: 16px;
    }
  }

}

.more {
  color: #6c5ce7;
  font-size: 20px;
  padding: 0 20px 20px;
}
</style>